package com.itany.shop.service.impl;

import com.itany.shop.constant.DictConstant;
import com.itany.shop.dao.ProductDao;
import com.itany.shop.entity.Product;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.util.PageInfo;
import com.itany.shop.util.ParameterUtil;

import java.util.List;

public class ProductServiceImpl implements ProductService {

    private ProductDao productDao = (ProductDao) ObjectFactory.getObject("productDao");

    @Override
    public List<Product> findAll() {
        return productDao.selectAll();
    }

    @Override
    public PageInfo<Product> findPage(String pageNo, String pageSize) {
        // 设置分页数据默认值
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.PRODUCT_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.PRODUCT_DEFAULT_PAGE_SIZE;
        }

        // 获取总记录条件
        int total = productDao.selectCount();
        // 获取当前页数据
        List<Product> products = productDao.selectPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        // 封装分页对象
        PageInfo<Product> productPage = new PageInfo<>(total, Integer.parseInt(pageNo), Integer.parseInt(pageSize), products);

        return productPage;
    }
}
